<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include_once(APPPATH."libraries/AdminController.php");
class Penutupan_formulir extends AdminController {  
	
	var $status_form = "'APPROVE','DONE'";
	var $dis = "";
	var $is_detail = FALSE;
	
	function __construct()   
	{
		parent::__construct();    
		$this->_set_action();
		$this->_set_action(array("detail"),"ITEM");
		$this->cat_search = array(
			'all'						=> 'Pilih Kategory',
			'form_principal_name'		=> 'Nama Principal',
			'form_obligee_name'			=> 'Nama Obligee',
			'form_bank_garansi_type'	=> 'Tipe Bank Garansi'
			/*'form_obligee_address'		=> 'Alamat Obligee',			
			'form_principal_address'	=> 'Alamat Principal',
			'form_document_number'		=> 'Nomor Dokumen',
			'form_document_desc'		=> 'Penjelasan Dokumen',
			'marketing_name'			=> 'Nama Marketing',
			'cur_name'					=> 'Nama Mata Uang'*/
		);
		$this->_set_title('Transaksi > Penutupan Formulir Asuransi');
		$this->_set_cat_search(array("all"	=> $this->cat_search));	
		$this->DATA->table="tm_formulir";
		$this->load->model("mdl_formulir_result","A");
		$this->folder_view = "transaksi/";
		$this->upload_path="./uploaded/formulir/";
		$this->prefix_view = strtolower($this->_getClass());
		
		//$this->output->enable_profiler(true);

	}

	function index(){
		
		$this->jCfg['isearch']['status'] = "all";
		$this->_releaseSession();
		
		$mst=isset($this->jCfg['isearch']['status'])?$this->jCfg['isearch']['status']:"all";
		$stat = ($mst=="all")?$this->status_form:"'".$mst."'";
		
		$this->per_page = $this->jCfg['isearch']['per_page'];
		$this->data_table = $this->A->data( array(
				"limit" 	=> $this->per_page, 
				"status"	=> $stat,
				"start_date"=> $this->jCfg['isearch']['start_date'],
				"end_date"	=> $this->jCfg['isearch']['end_date'],
				"offset" 	=> $this->uri->segment($this->uri_segment)
		) );
		
		$data = $this->_data(array(
				'base_url'		=> $this->own_link.'index'
			)
		);	
					
		$this->_v($this->folder_view.$this->prefix_view,$data);
	}
	
	function get_note($form_id="",$asuransi_id = ""){		
		$this->output->enable_profiler(FALSE);
		if(trim($form_id) !="" && trim($asuransi_id) != ""){
			$this->DATA->table = "tm_pialang_note";
			$m = $this->db->query("
				select * from tm_pialang_note where note_form_id = '".dbClean($form_id)."'
				AND note_as_id = '".dbClean($asuransi_id)."'
				order by note_id DESC
			")->row();
			if($this->jCfg['user']['id'] == $m->note_receiver){
				$this->DATA->_update(array(
					"note_form_id"		=> dbClean($form_id),
					"note_as_id"		=> dbClean($asuransi_id)
				),array(
					"note_read"	=> 1
				));
			}
			$mm = get_note(array(
				  	"form_id"		=> dbClean($form_id),
					"asuransi_id"	=> dbClean($asuransi_id)
				  ));
			$this->_v("note",array('data'=>$mm),FALSE);
		}else{
			echo "empty";
		}
	}
	
	function search(){
		//$this->_set_desc_title('Daftar Penawaran Formulir Kepada Asuransi');	
		$this->load->helper('url');		
		$this->uri_segment = 6;
			
		$column = ''; $keyword = '';				
		if( isset($_POST['column']) && isset($_POST['keyword']) ){
			$column = $_POST['column'];
			$keyword = url_title($_POST['keyword']);
			$this->setiSearch(array(
				"start_date"=> dbClean($_POST['dstart']),
				"end_date"	=> dbClean($_POST['dend']),
				"status"	=> dbClean($_POST['status']),
				"per_page"	=> dbClean($_POST['per_page'])
			));
		}else{
			$column =  $this->uri->segment(4);
			$keyword =   $this->uri->segment(5);
		}	
		
		$stat = ($this->jCfg['isearch']['status']=="all")?$this->status_form:"'".$this->jCfg['isearch']['status']."'";
		
		$this->per_page = $this->jCfg['isearch']['per_page'];
		$this->data_table = $this->A->data( array(
								"limit" 	=> $this->per_page, 
								"offset" 	=> $this->uri->segment($this->uri_segment),
								"column"	=> $column,
								"start_date"=> $this->jCfg['isearch']['start_date'],
								"end_date"	=> $this->jCfg['isearch']['end_date'],
								"status"	=> $stat,
								"keyword"	=> (trim($keyword)=="all")?"":$keyword
							) );
		$k=($keyword=="")?"all":$keyword;
		$data = $this->_data(
			array(
				'base_url'		=>$this->own_link.'search/'.$column.'/'.$k
			)
		);	
		
		
		$this->header_page['key']=pTxt(($k=="all")?"":$k);
		$this->header_page['col']=$column;	
		$this->header_page['cat_search'] = array($column	=>	$this->cat_search);	
		$this->_v($this->folder_view.$this->prefix_view,$data);
	}
		
	function edit($id=''){
		$id=dbClean(trim($id));		
		if(trim($id)!=''){
			$mdata = $this->A->data(array(
					'form_id'	=> $id
				));
			
			if(count($mdata['data'])==0)
				redirect($this->own_link);
				
			$this->data_form = $mdata['data'][0];
			
			$this->DATA->table = "tm_pialang_marketing";
			$marketing = $this->DATA->_getall();
			
			$this->DATA->table = "tm_currency";
			$curr = $this->DATA->_getall();
			
			$this->DATA->table = "tm_bank";
			$bank = $this->DATA->_getall();
			
			$this->DATA->table = "tm_file";
			$file = $this->DATA->_getall(array(
				"file_form_id"	=> $id
			));
										
			$this->_v($this->folder_view.$this->prefix_view."_form",array(
					"marketing"	=> $marketing,
					"curr"		=> $curr,
					"bank"		=> $bank,
					"files"		=> $file
			));
		}else{
			redirect($this->own_link);
		}
	}	
	
	function detail($id=''){
		$this->is_detail = TRUE;
		$this->dis = 'disabled="disabled"';
		
		$id=dbClean(trim($id));		
		if(trim($id)!=''){
			$mdata = $this->A->data(array(
					'form_id'	=> $id
				));
			
			if(count($mdata['data'])==0)
				redirect($this->own_link);
				
			$this->data_form = $mdata['data'][0];
			
			$this->DATA->table = "tm_pialang_marketing";
			$marketing = $this->DATA->_getall();
			
			$this->DATA->table = "tm_currency";
			$curr = $this->DATA->_getall();
			
			$this->DATA->table = "tm_bank";
			$bank = $this->DATA->_getall();
			
			$this->DATA->table = "tm_file";
			$file = $this->DATA->_getall(array(
				"file_form_id"	=> $id
			));
										
			$this->_v($this->folder_view.$this->prefix_view."_form",array(
					"marketing"	=> $marketing,
					"curr"		=> $curr,
					"bank"		=> $bank,
					"files"		=> $file
			));
		}else{
			redirect($this->own_link);
		}
	}	
	
	function save(){
		$data = array(
			'form_bank_penerbit' 		=> dbClean($_POST['form_bank_penerbit']),
			'form_no_bank_garansi' 		=> dbClean($_POST['form_no_bank_garansi']),
			'form_tgl_terbit_bank_garansi' 		=> dbClean($_POST['form_tgl_terbit_bank_garansi']),
			'form_no_invoice' 			=> dbClean($_POST['form_no_invoice']),
			'form_tgl_invoice' 			=> dbClean($_POST['form_tgl_invoice']),
			'form_client_rate' 			=> dbClean($_POST['form_client_rate']),
			'form_biaya_ditagihkan_ke_client' 	=> dbClean($_POST['form_biaya_ditagihkan_ke_client']),
			'form_biaya_notaril' 		=> dbClean($_POST['form_biaya_notaril']),
			'form_biaya_adm' 			=> dbClean($_POST['form_biaya_adm']),
			'form_jenis_pembayaran' 	=> dbClean($_POST['form_jenis_pembayaran']),
			'form_tgl_pembayaran' 		=> dbClean($_POST['form_tgl_pembayaran']),
			'form_marketing' 			=> dbClean($_POST['form_marketing']),
			'form_jumlah_pembayaran' 	=> dbClean($_POST['form_jumlah_pembayaran']),
			'form_biaya_provisi' 		=> dbClean($_POST['form_biaya_provisi']),
			'form_biaya_adm_asuransi' 	=> dbClean($_POST['form_biaya_adm_asuransi']),
			'form_biaya_notaril_asuransi' 		=> dbClean($_POST['form_biaya_notaril_asuransi']),
			'form_jenis_pembayaran_asuransi' 	=> dbClean($_POST['form_jenis_pembayaran_asuransi']),
			'form_tgl_bayar_asuransi' 			=> dbClean($_POST['form_tgl_bayar_asuransi']),
			'form_jumlah_bayar_asuransi'=> dbClean($_POST['form_jumlah_bayar_asuransi']),
			'form_rate_bank'=> dbClean($_POST['form_rate_bank']),
			'form_komisi' 				=> dbClean($_POST['form_komisi']),
			'form_status' 				=> dbClean($_POST['form_status'])
		);

		$a = $this->_save_master( 
			$data,
			array(
				'form_id' => dbClean($_POST['form_id'])
			),
			dbClean($_POST['form_id'])			
		);
		
		foreach($_FILES as $k => $v){
			$this->upload_formulir(
				array(
					'id'		=> $a['id'],
					'input'		=> $k
				)
			);
		}
		
		redirect($this->own_link);
	}
	
	function upload_formulir($par=array()){
		if($_FILES[$par['input']]['error']==4)
			return false;
		
		$uId = rand(1,100000)."-".uniqid();
		$uri = $this->upload_path;
		$location = $uri.""."bukti";
		$nlocation = $location."/".$par['id'];
		
		if(!is_dir($location)){
			@mkdir($location);
		}
		
		if(!is_dir($nlocation)){
			@mkdir($nlocation);
		}
		
		$fileName = $uId;
		$config['upload_path'] 		= $nlocation."/";
		$config['file_name'] 		= $fileName;
		$config['allowed_types'] 	= 'doc|docx|xls|xlsx|pdf|zip|rar|gif|jpg|png';
		$config['max_size']			= 1024*10;
		$this->load->library('upload');
		
		$this->upload->initialize($config);
	
		if( $this->upload->do_upload($par['input']) )
		{	
			$file = $uId.$this->upload->file_ext;		
			$this->DATA->_update(array(
				"form_id"	=> $par['id']
			),array(
				"form_bukti_pembayaran"	=> $file
			));
		}
	}
		
}
